var HeScroll = null;
(function() {
	var page = pf.newPage('features', true);
	var $el  = null;
	page.car = ''

	/**
	 * 初始化时调用
	 */
	 function tf(es){
	var r= /^[+-]?[1-9]?[0-9]*\.[0-9]*$/;
    return r.test(es);
	}
	page.onCreate = function() {
		$el = page.$el;
		HeScroll = page.HeScroll = new iScroll("scroller_three",{
			momentum:    false,
			snap    :     true,
			vScroll:    false,
			vScrollbar: false,
			hScrollbar: false,
			click:      true
		});
		$el.on('click',".features_button_left",function(){
			$('.features_video').empty();
			$('#features').removeClass('test_video_now');
			pcc2.css3(".features_button_left.hi-icon.hi-icon-mobile.features_button.porsche_image");
			$el.find(".features_button").css("opacity","1");
			if (Math.abs(HeScroll.currPageX)==0) {
				$el.find($(this)).css("opacity","0");
			}
			if (Math.abs(HeScroll.currPageX)==1) {
				$el.find($(this)).css("opacity","0");
			}
			if (Math.abs(HeScroll.currPageX)==0) {
				// HeScroll.scrollToPage(2,0,0);
			}else if(!tf(Math.abs(HeScroll.currPageX))){
				HeScroll.scrollToPage(Math.abs(HeScroll.currPageX)-1,0,500);
			}
		});
		$el.on('click',".features_button_right",function(){
			pcc2.css3(".features_button_right.hi-icon.hi-icon-mobile.features_button.porsche_image");
			$el.find(".features_button").css("opacity","1");
			if (Math.abs(HeScroll.currPageX)+2>2) {
				$el.find($(this)).css("opacity","0");
				// $('#features').addClass('features_hide_back');
				// document.getElementsByTagName('video')[0].pause();
				$('.features_video').append('<video  class="features_background" style="z-index:100;  width: 100%;height: 100%;" loop="loop"  autoplay="autoplay"><source src="./video/991_911-Targa-4-GTS_Drivingscenes_1280_L.mp4" type="video/mp4" /></video>');
				$('#features').addClass('test_video_now');
			}
			if (Math.abs(HeScroll.currPageX)==2) {
				// HeScroll.scrollToPage(0,0,0);
			}else if(!tf(Math.abs(HeScroll.currPageX))){
				lr = window.setTimeout(function() {
					HeScroll.scrollToPage(Math.abs(HeScroll.currPageX)+1,0,500);
				window.clearTimeout(lr);
				}, 100);
			}
		});
	};

	/**
	 * 页面显示时自动调用
	 */
	 function change(action){
	 	$el.find('.car_h').html(__(action+"_h"));
		// $el.find('.car_width').html(__(action+"_width"));
		$el.find('.car_w').html(__(action+"_w"));
		$el.find('.car_l').html(__(action+"_l"));
		$el.find('.car_po').html(__(action+"_po"));
		$el.find('.car_a').html(__(action+"_a"));
		$el.find('.car_t').html(__(action+"_t"));
		$el.find('.car_f').html(__(action+"_f"));
		$el.find('.car_e').html(__(action+"_e"));
		$el.find('.car_p').html(__(action+"_p"));
	 }
	page.onShow = function(action) {
		page.car = action.page_car;
		switch(action.lang_key) {
			case '911_carrera_S':
				 $('#container').addClass('911_gt3');
				 pcc2.light('on',1);
			break;
			case 'Cayenne_Turbo_S':
				 $('#container').addClass('cayenne_turbo_s');
				 pcc2.light('on',2);
			break;
			case 'panamera_Turbo_Executive':
				 $('#container').addClass('panamera_turbo_s');
				 pcc2.light('on',3);
			break;
		}
		// text_features_center_iscroll3_img
		$el.find(".text_features_center_iscroll3_img").css("background-image","url(./image/qrcode/"+action.page_car+".png)");
		$("body").addClass("white_border");
		// $el.show();
		// alert(action.car_name);
		var car_one="";
	 	$el.find('.features_center_iscroll1_big_img').css('background','url(./image/specs/car_specs_'+action.car_name+'_1.png)')
		// switch(action.car_name){
		// 	case "911":
		// 	action.car_name="911_carrera";
		// 	// car_one="911 Carrera";
		// 	break;
		// 	case "boxster":
		// 	action.car_name="boxster";
		// 	// car_one="Boxster";
		// 	break;
		// 	case "cayman":
		// 	action.car_name="cayman";
		// 	// car_one="Cayman";
		// 	break;
		// 	case "cayenne":
		// 	action.car_name="Cayenne";
		// 	// car_one="Cayenne";
		// 	break;
		// 	case "macan":
		// 	action.car_name="Macan";
		// 	// car_one="Macan";
		// 	break;
		// 	case "panamera":
		// 	action.car_name="panamera";
		// 	// car_one="Panamera";
		// 	break;
		// 	default:
		// 	// alert("这车失踪了");
		// 	break;
		// }
	 	$('.features_center_iscroll1_big_tittle,.features_center_iscroll2_big_tittle').text(action.tittle);
		change(action.lang_key);
		window.setTimeout(function() {
			HeScroll.refresh();
		}, 1000);
	};

	/**
	 * 页面隐藏时调用
	 */
	page.onHide = function() {
		if ($('#container').hasClass('911_gt3')) {
			pcc2.light('off',1);
			$('#container').removeClass('911_gt3');
		};
		if ($('#container').hasClass('cayenne_turbo_s')) {
			pcc2.light('off',2);
			$('#container').removeClass('cayenne_turbo_s');
		};
		if ($('#container').hasClass('panamera_turbo_s')) {
			pcc2.light('off',3);
			$('#container').removeClass('panamera_turbo_s');
		};

		// $("body").removeClass("white_border");
		HeScroll.scrollToPage(0);
		$el.find(".features_button").css("opacity","1");
		$el.find($(".features_button_left")).css("opacity","0");
		HeScroll.refresh();
		// $el.hide();
		page.car = '';
		$('.features_video').empty();
		$('#features').removeClass('test_video_now');
			if (pcc2.$video.is(":hidden")) {
				if ($("#features").find("video").length>0) {
					$("#features").find("video").trigger('play');
				};
				if ($("#detail").find("video").length>0) {
					$("#detail").find("video").eq(0).trigger('play');
				};
			}else{
				pcc2.$video.find("video").trigger('play');
			}
	};

})();